import bokeh.palettes, os
from strange_io.colors import StrangeColors
from protolib import hex_to_rgb, get_app_root

os.makedirs(os.path.join(get_app_root(), 'palettes', 'bokeh'), exist_ok=True)

def dump(name: str, palette: list[str], directory: str = 'palettes'):
    try:
        rgb_palette = []
        for color in palette:
            rgb_palette.append(hex_to_rgb(color))
        scolors = StrangeColors(rgb_palette)
        scolors.dump_file(directory + '/' + name + '.scolors')
    except ValueError:
        print('Failed to dump palette \'%s\'' % name)

def dump_dict(palettes, directory: str = 'palettes'):
    for k in palettes:
        v = palettes[k]
        if isinstance(v, list) or isinstance(v, tuple):
            dump(k, v, directory)
        elif isinstance(v, dict):
            for k2 in v:
                v2 = v[k2]
                dump('%s %s' % (k, k2), v2, directory)
        else:
            raise TypeError('Invalid type')

dump_dict(bokeh.palettes.all_palettes, 'palettes/bokeh')
dump('default', ['#000000', '#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#00FFFF', '#FFFFFF'])
